package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussInspectionPlanDao;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussInspectionPlanEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussInspectionPlanService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysUserService;

@Service("bussInspectionPlanService")
public class BussInspectionPlanServiceImpl extends ServiceImpl<BussInspectionPlanDao, BussInspectionPlanEntity>
		implements BussInspectionPlanService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;
	@Autowired
	private SysDeptService sysDeptService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String name = (String) params.get("name");

		IPage<BussInspectionPlanEntity> page = this.page(new Query<BussInspectionPlanEntity>().getPage(params),
				new QueryWrapper<BussInspectionPlanEntity>()
						.like(StringUtils.isNotBlank(name), "name", name)
						// 个人数据权限控制
						.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
								"dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
						.orderByDesc("create_time")
		);
		for (BussInspectionPlanEntity bipe : page.getRecords()) {
			if (StringUtils.isNotBlank(bipe.getHandler())) {// 巡检人
				SysUserEntity user = sysUserService.getById(bipe.getHandler());
				if (user != null && StringUtils.isNotBlank(user.getName())) {
					bipe.setHandlerName(user.getName());
				}
			}
			if (StringUtils.isNotBlank(bipe.getBussFarmlandInfoIds())) {// 田块
				String tkNames = "";
				String newstr = "";
				List<String> nlist = new ArrayList<>();
				if (bipe.getBussFarmlandInfoIds().indexOf(",") > 0) {

					String[] idsArr = bipe.getBussFarmlandInfoIds().split(",");

					String[] a = bipe.getBussFarmlandInfoIds().split(",");
					for (int i = 0; i < a.length; i++) {
						newstr = newstr + "'" + a[i] + "',";
						nlist.add(a[i]);
					}
					newstr = newstr.substring(0, newstr.length() - 1);
				} else {
					newstr = bipe.getBussFarmlandInfoIds();
				}
				QueryWrapper<BussFarmlandInfoEntity> ew = new QueryWrapper<BussFarmlandInfoEntity>();
				if (nlist.size() == 0) {
					ew.in("id", bipe.getBussFarmlandInfoIds());
				} else {
					ew.inOrThrow("id", nlist);
				}

				// ew.selectBatchIds("id", nlist);
				// List<BussFarmlandInfoEntity> nb = bussFarmlandInfoService.

				List<BussFarmlandInfoEntity> list = bussFarmlandInfoService.list(ew);
				if (list.size() > 0) {
					for (BussFarmlandInfoEntity tk : list) {
						tkNames += tk.getName() + ",";
					}
					tkNames = tkNames.substring(0, tkNames.length() - 1);
				}

				bipe.setTkNames(tkNames);
			}
			if (StringUtils.isNotBlank(bipe.getDeptId())) {// 部门
				SysDeptEntity dept = sysDeptService.getById(bipe.getDeptId());
				if (dept != null && StringUtils.isNotBlank(dept.getName())) {
					bipe.setDeptName(dept.getName());
				}
			}
		}
		return new PageUtils(page);
	}

}
